package org.example.demo3.controller;

import jakarta.servlet.http.HttpSession;
import org.example.demo3.entity.Survey;
import org.example.demo3.entity.User;
import org.example.demo3.service.SurveyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
@RequestMapping("/survey")
public class SurveyController {

    @Autowired
    private SurveyService surveyService;

    @GetMapping("/list")
    public String list(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("surveys", surveyService.getAllSurveys());
        model.addAttribute("user", user);
        return "survey/list";
    }

    @GetMapping("/create")
    public String createPage(Model model) {
        model.addAttribute("survey", new Survey());
        return "survey/create";
    }

    @PostMapping("/create")
    public String create(@Validated Survey survey, HttpSession session) {
        User user = (User) session.getAttribute("user");
        survey.setUserId(user.getId());
        survey.setCreateTime(new Date());  // 设置创建时间
        surveyService.createSurvey(survey);
        return "redirect:/survey/list";
    }

    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Survey survey = surveyService.getSurveyById(id);
        if (survey == null) {
            model.addAttribute("error", "问卷不存在");
            return "error";
        }
        model.addAttribute("survey", survey);
        model.addAttribute("user", user);
        return "survey/detail";
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable("id") Integer id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Survey survey = surveyService.getSurveyById(id);
        if (survey == null || !survey.getUserId().equals(user.getId())) {
            return "fail";
        }
        surveyService.deleteSurvey(id);
        return "success";
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Survey survey = surveyService.getSurveyById(id);
        if (survey == null || !survey.getUserId().equals(user.getId())) {
            model.addAttribute("error", "无权限编辑此问卷");
            return "error";
        }
        model.addAttribute("survey", survey);
        return "survey/edit";
    }

    @PostMapping("/update/{id}")
    public String update(@PathVariable("id") Integer id, @Validated Survey survey, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Survey existingSurvey = surveyService.getSurveyById(id);
        if (existingSurvey == null || !existingSurvey.getUserId().equals(user.getId())) {
            return "redirect:/survey/list";
        }
        survey.setId(id);
        survey.setUserId(user.getId());
        surveyService.updateSurvey(survey);
        return "redirect:/survey/list";
    }

    @ExceptionHandler(Exception.class)
    public String handleError(Exception e, Model model) {
        model.addAttribute("error", e.getMessage());
        return "error";
    }
}